package cn.springmvc.controller;

import static org.junit.Assert.assertNotNull;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/activiti")
public class ProcessDefinitionController {
	
	private RepositoryService  repositoryService;
	private RuntimeService  runtimeService;
	
	/**
     * 
     *
     *<p>Description:部署流程定义</p>
     *
     * @author：SongJia
     *
     * @date: 2017-3-14下午4:12:31
     *
     */
    @RequestMapping(value = "/deploymentProcessDefinition")
    @ResponseBody
    public String deploymentProcessDefinition(String processname,String deploymentname,HttpServletRequest request){
    	String result="fail";
    	String fileDir="diagrams/";
    	String realPath =this.getClass().getClassLoader().getResource("").getPath();
    	String filename=realPath.concat(fileDir).concat(processname).concat(".bpmn");
    	try {
			repositoryService.createDeployment().addInputStream(processname+".bpmn20.xml",
					new FileInputStream(filename)).deploy();
		} catch (FileNotFoundException e) {
			result="success";
			e.printStackTrace();
		}
		Map<String, Object> variableMap = new HashMap<String, Object>();
		variableMap.put("name", "Activiti");
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processname, variableMap);
		assertNotNull(processInstance.getId());
		System.out.println("id " + processInstance.getId() + " "
				+ processInstance.getProcessDefinitionId());
		return result;

    }
    
    
    public String deployementProcessDefinitionByzip(HttpServletRequest req){
    	return null;
    }  

}
